package com.dyh.quickdemo.bizThreLoan;

import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

@Slf4j
public class BizThreadLoan {
    public static final Map<String, AtomicInteger> topic2num = new HashMap<>();
    static {
        topic2num.put("hello",new AtomicInteger(1));
    }

    public static boolean apply(String topic){
        AtomicInteger num;
        if ((num= topic2num.get(topic) )==null){
            log.info("找不到topic");
            return false;
        }
        log.info("{}",num.get());
        if (num.get()==0)
            return false;
        return num.decrementAndGet()>=0;
    }

    public static boolean repay(String topic){
        AtomicInteger num;
        if ((num= topic2num.get(topic) )==null){
            log.info("找不到topic");
            return false;
        }
        log.info("{}",num.get());
        return num.incrementAndGet()>=0;
    }

    /*
     17:17:29.029 [Thread-1] INFO com.dyh.quickdemo.bizThreLoan.BizThreadLoan - 1
    17:17:29.029 [Thread-2] INFO com.dyh.quickdemo.bizThreLoan.BizThreadLoan - 1
    17:17:29.034 [Thread-2] INFO com.dyh.quickdemo.bizThreLoan.BizThreadLoanTest - false
    17:17:29.034 [Thread-1] INFO com.dyh.quickdemo.bizThreLoan.BizThreadLoanTest - true

    num.decrementAndGet()>=0; 的必要性
    */
}
